package com.peng.community.controller;

import com.peng.community.service.AlphaService;
import com.peng.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * 测试代码
 */

@Controller
@RequestMapping("/alpha")
public class AlphaController {

    @Autowired
    private AlphaService alphaService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "Hello SpringBoot Demo.";
    }

    @RequestMapping("/data")
    @ResponseBody
    public String getData() {
        return alphaService.find();
    }

    @RequestMapping("/http")
    public void http(HttpServletRequest request, HttpServletResponse response) {
        //获取请求数据
        System.out.println(request.getMethod());
        System.out.println(request.getServletPath());
        //请求消息头
        Enumeration<String> enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();
            String value = request.getHeader(name);
            System.out.println(name + ": " + value);
        }
        System.out.println(request.getParameter("code"));

        // 返回响应数据
        response.setContentType("text/html;charset=utf-8");
        try (
                PrintWriter writer = response.getWriter();
        ) {
            writer.write("<h1>鹏飞12138</h1>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //Get请求：获取某些数据
    // /students?current=1&limit=20
//    @GetMapping("/students")
    @RequestMapping(path = "/students", method = RequestMethod.GET)
    @ResponseBody
    public String students(
            @RequestParam(name = "current", required = false, defaultValue = "1") int current,
            @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
        System.out.println(current);
        System.out.println(limit);
        return "some students";
    }

    // /student/1
    @GetMapping("/student/{id}")
    @ResponseBody
    public String getstduent(@PathVariable("id") int id) {
        System.out.println(id);
        return "a stduent";
    }

    //post请求
    @PostMapping("/student")
    @ResponseBody
    public String savestudent(String name, int age) {
        System.out.println(name);
        System.out.println(age);
        return "success";
    }

    //响应html数据
    @GetMapping("/teacher")
    public ModelAndView getTeacher() {
        ModelAndView model = new ModelAndView();
        model.addObject("name", "张三");
        model.addObject("age", 25);
        model.setViewName("/demo/view");
        return model;
    }

    @GetMapping("/school")
    public String getSchool(Model model) {
        model.addAttribute("name", "斯阿鬼");
        model.addAttribute("age", 108);
        return "/demo/view";
    }

    //响应json数据（异步请求）
    // java对象-》json字符串 -》js对象
    @GetMapping("/emp")
    @ResponseBody
    public Map<String, Object> getEmp() {
        HashMap<String, Object> emp = new HashMap<>();
        emp.put("name", "张三");
        emp.put("age", 23);
        emp.put("salary", "8000");
        return emp;
    }

    @GetMapping("/emps")
    @ResponseBody
    public List<Map<String, Object>> getEmps() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> emp = new HashMap<>();
        emp.put("name", "张三");
        emp.put("age", 23);
        emp.put("salary", "8000");
        list.add(emp);

        emp.put("name", "李四");
        emp.put("age", 33);
        emp.put("salary", "10000");
        list.add(emp);

        emp.put("name", "程序员");
        emp.put("age", 23);
        emp.put("salary", "100000");
        list.add(emp);
        return list;
    }

    /**
     * cookie示例
     */
    @GetMapping("/cookie/set")
    @ResponseBody
    public String setCookie(HttpServletResponse response) {
        //创建cookie
        Cookie cookie = new Cookie("code", CommunityUtil.generateUUID());
        //设置cookie生效的范围
        cookie.setPath("/community/alpha");
        //cookie生存时间
        cookie.setMaxAge(60 * 10);
        //发送cookie
        response.addCookie(cookie);
        return "set cookie";
    }
    @GetMapping("/cookie/get")
    @ResponseBody
    public String getCookie(@CookieValue("code") String code){
        System.out.println(code);
        return "get Cookie";
    }

    /**
     * session示例
     */
    @GetMapping("/session/set")
    @ResponseBody
    public String setSession(HttpSession session){
        session.setAttribute("id",1);
        session.setAttribute("name","pengfei");
        return "set session";
    }
    @GetMapping("/session/get")
    @ResponseBody
    public String getSession(HttpSession session){
        System.out.println(session.getAttribute("id"));
        System.out.println(session.getAttribute("name"));
        return "get session";
    }

    //ajax示例
    @PostMapping("/ajax")
    @ResponseBody
    public String ajax(String name,int age){
        System.out.println(name);
        System.out.println(age);
        return CommunityUtil.getJSONString(0,"操作成功");
    }
}
